package com.blog.cmrpersonalblog.strategy.upload.impl;

import com.blog.cmrpersonalblog.enums.FileUploadType;
import com.blog.cmrpersonalblog.strategy.upload.AbstractFileUploadStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

/**
 * 通用图片上传策略
 */
@Slf4j
@Component
public class GeneralImageUploadStrategy extends AbstractFileUploadStrategy {
    
    @Override
    public FileUploadType getSupportedType() {
        return FileUploadType.GENERAL_IMAGE;
    }
    
    @Override
    public boolean isValidFileType(MultipartFile file) {
        // 通用图片支持所有图片格式
        String extension = getFileExtension(file.getOriginalFilename());
        return SUPPORTED_IMAGE_TYPES.contains(extension.toLowerCase());
    }
    
    @Override
    public String generateFileName(String originalFileName, Long userId, Object... extraParams) {
        // 通用图片命名规则：img_用户ID_时间戳_随机码.扩展名
        String extension = getFileExtension(originalFileName);
        String timestamp = String.valueOf(System.currentTimeMillis());
        String randomCode = java.util.UUID.randomUUID().toString().replace("-", "").substring(0, 8);
        
        if (userId != null) {
            return String.format("img_user%d_%s_%s.%s", userId, timestamp, randomCode, extension);
        } else {
            return String.format("img_%s_%s.%s", timestamp, randomCode, extension);
        }
    }
}
